import * as NSBase from '../../Common/NetServiceBase';
import { Random, IsNullOrWhiteSpace } from '../../TYLibrary/Common/GlobalExtend';
import {
    TYConfig,
    Wx_Page,
    Wx_PageData,
    WXDataClassRuntimeDecorator,
    Logger,
    LogLevel
} from '../EntityBase';

//NS.GetOpenId
//NS.QueryIsAgree
@WXDataClassRuntimeDecorator()
export class LoadingBasePageData extends Wx_PageData {
    public LoadFail: boolean = false;
    public ErrMessage: string = "";
    public ShowLoadimg: string = "";
}
export class LoadingBasePage extends Wx_Page {
    /**
     * 引导页最少显示时间（秒）
     */
    private m_Showtime: number = 3;

    //加载页图片
    public loadimgs: Array<string> = ['/img/loading01.jpg', '/img/loading02.jpg'];
    /**
     * 首页
     */
    //public IndexPageType: PageType = PageType.AppIndex;

    public Data = new LoadingBasePageData();

    private _Movetimes(): void {
        let r = setTimeout(() => {
            this.m_Showtime = 0;
        }, this.m_Showtime * 1000);
    }

    /**
     * 初始化页面信息
     * 
     * @memberof indexPage
     */
    public async InitData(options: any) {
        this.Data.ShowLoadimg = this.loadimgs[Random(0, this.loadimgs.length)];
        await this.Data.ApplyChange(this);
        this._Movetimes();
        try {
            wx.showNavigationBarLoading();
            wx.setNavigationBarTitle({ title: "加载中,请稍侯..." });
            if (!IsNullOrWhiteSpace(this.App.AgreementPageType)) {
                //let oldIsAgree = wx.getStorageSync('UserIsAgree');
                //if (!oldIsAgree) {
                let OpenId = await NSBase.GetOpenId(TYConfig.GetOpenId);
                let IsAgree = await NSBase.QueryIsAgree(TYConfig.QueryIsAgree, OpenId);
                if (!IsAgree) {
                    wx.reLaunch({ url: this.App.AgreementPageType });
                    return;
                }
                else
                    wx.setStorageSync('UserIsAgree', IsAgree);
                //}
            }

            this.m_App_InitUserInfoAndCheckAuthInfo(async () => {
                await this.OnAppInitReady();
                await this.ShowIndex();
            });
        } catch (e) {
            Logger.Error(LogLevel.L1, e);
        } finally {
            wx.setNavigationBarTitle({ title: "" });
            wx.hideNavigationBarLoading();
        }
    }

    /**
     * APP初始化完成
     *
     * @memberof indexPage
     */
    public async OnAppInitReady() { }

    public async ShowIndex(): Promise<void> {
        return new Promise<void>((resolve, reject) => {
            let r = setInterval(() => {
                if (this.m_Showtime <= 0) {
                    clearInterval(r);
                    if (IsNullOrWhiteSpace(this.App.IndexPageType))
                        throw "IndexPageType属性不能为空!";
                    else
                        wx.switchTab({ url: this.App.IndexPageType });
                    resolve();
                }
            }, 1000);
        });
    }

    public async RetryHandler() {
        this.Data.LoadFail = false;
        await this.Data.ApplyChange(this);
        await this.InitData(null);
    }
}
